package bilibli.utils;

import bilibli.pojo.Video;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class ParseVideoUtil {

    public static Video parseVideo(String aid) {

        //初始化视频对象
        Video video = new Video();
        //获取视频url 返回json拥有首先要的数据 aid 和 bid 和视频标题
        try{
            String url = "https://api.bilibili.com/x/web-interface/view?aid=" + aid;
            String json = parsePath(url);
            //转换成json
            JSONObject jsonObject = JSON.parseObject(json);
            JSONObject jsonData = jsonObject.getJSONObject("data");
            JSONArray jsonArray = jsonData.getJSONArray("pages");
            //获取bid
            String bid = (String) jsonData.get("bvid");
            //获取aid
            Integer avid = (Integer) jsonData.get("aid");
            Map map = (Map) jsonArray.get(0);
            //获取cid
            Integer cid = (Integer) map.get("cid");
            //获取视频标题
            String title = (String) jsonData.get("title");
            video.setAid(avid);
            video.setCid(cid);
            video.setBid(bid);
            video.setTitle(title);
            return video;
        }catch (NullPointerException e){
            System.out.println("资源失效");
        }
        return null;
    }

    //解析路径返回json数据
    public static String parsePath(String url) {
        String jsonText = null;
        Connection connection = Jsoup.connect(url).timeout(3000).ignoreContentType(true);
        Map<String, String> heads = new HashMap<>();
        //伪造浏览器数据
        heads.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
        heads.put("Accept-Encoding", "gzip, deflate, br");
        heads.put("Accept-Language", "en,zh-CN;q=0.9,zh;q=0.8");
        heads.put("Cache-Control", "max-age=0");
        heads.put("Connection", "keep-alive");
        //TODO 请在这里填写自己的cookie,没有cookid将会请求不到
        heads.put("Cookie", "_uuid=CEFF0C7B-6D58-0398-21C6-3FC333B36C6E48488infoc; buvid3=EE47F094-4FB9-4B32-B4FE-8FB7F0231BBB155819infoc; sid=9oh8mjjy; CURRENT_FNVAL=16; rpdid=|(um|||kRRmR0J'ul)JJ)~mRk; LIVE_BUVID=AUTO2715833879432735; html5_player_gray=false; LNG=zh-CN; DedeUserID=317805262; DedeUserID__ckMd5=dd7f816655d50616; SESSDATA=dea1b0d5%2C1610541690%2C16c42*71; bili_jct=1743f5d024e8f3bdc9160fddd5df0fab; bp_video_offset_317805262=413906595317535077; CURRENT_QUALITY=116; flash_player_gray=false; PVID=1; bfe_id=018fcd81e698bbc7e0648e86bdc49e09");
        heads.put("Host", "api.bilibili.com");
        heads.put("Sec-Fetch-Mode", "navigate");
        heads.put("Sec-Fetch-Site", "none");
        heads.put("Sec-Fetch-User", "?1");
        heads.put("Upgrade-Insecure-Requests", "1");
        heads.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
        connection.headers(heads);
        try {
            jsonText = connection.get().text();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("建立获取cid连接失败");
        }
        return jsonText;
    }


    public static String getURL(Video video) {
        String url = "https://api.bilibili.com/x/player/playurl?cid="+video.getCid()+"&fnval=0&fnver=0&bvid="+video.getBid()+"&otype=json&qn=80&player=1&avid="+video.getAid()+"";
        return url;
    }

    public static String[] getVideoUrl(String url){
        String json = parsePath(url);
        JSONObject jsonObject = JSON.parseObject(json);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray durl = data.getJSONArray("durl");
        Map map = (Map) durl.get(0);
        String o = (String) map.get("url");
        Integer c = (Integer) map.get("size");
        return new String[]{o, String.valueOf(c)};
    }


}
